package com.mes.web.controller.biz;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.mes.biz.domain.BizWarehouse;
import com.mes.common.annotation.Log;
import com.mes.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.page.PageDomain;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import javax.validation.constraints.*;

import com.mes.system.strategy.AutoCodeUtil;
import org.springframework.web.bind.annotation.*;
import com.mes.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.domain.BizSupplier;
import com.mes.biz.service.BizSupplierService;

/**
 * 供应商档案Controller
 * @author hwg
 * @email hwg
 * @date 2023-04-30
 */
@RestController
@RequestMapping("/biz/bizSupplier")
public class BizSupplierController extends BaseController {

    @Autowired
    private BizSupplierService bizSupplierService;

    @Resource
    private AutoCodeUtil autoCodeUtil;

    /**
     * 查询供应商档案列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizSupplier:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizSupplier bizSupplier, HttpServletRequest request, HttpServletResponse response) {
        bizSupplier.setPage(new PageDomain(request, response));
        return R.data(bizSupplierService.findPage(bizSupplier));
    }

    /**
     * 查询所有供应商列表
     */
    @GetMapping("/allList")
    public R<List<BizSupplier>> list() {
        return R.data(bizSupplierService.findList(null));
    }

    /**
     * 获取供应商档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizSupplier:query')")
    @GetMapping(value = "/{id}")
    public R<BizSupplier> detail(@PathVariable("id") String id) {
        return R.data(bizSupplierService.get(id));
    }

    /**
     * 新增供应商档案
     */
    @PreAuthorize("@ss.hasPermi('biz:bizSupplier:add')")
    @Log(title = "供应商档案", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizSupplier bizSupplier) {
        bizSupplier.setTenantId(SecurityUtils.getTenantId());
        String supplierCode  = autoCodeUtil.genSerialCode("SUPPLIER_CODE",null);
        bizSupplier.setSupplierCode(supplierCode);
        return R.status(bizSupplierService.save(bizSupplier));
    }

    /**
     * 修改供应商档案
     */
    @PreAuthorize("@ss.hasPermi('biz:bizSupplier:edit')")
    @Log(title = "供应商档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizSupplier bizSupplier) {
        return R.status(bizSupplierService.save(bizSupplier));
    }

    /**
     * 删除供应商档案
     */
    @PreAuthorize("@ss.hasPermi('biz:bizSupplier:remove')")
    @Log(title = "供应商档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizSupplierService.deleteBizSupplierByIds(ids));
    }


    /**
     * 校验供应商名称的唯一性
     */
    @PreAuthorize("@ss.hasPermi('biz:bizSupplier:query')")
    @Log(title = "供应商档案", businessType = BusinessType.CHECK)
    @GetMapping(value = {"/checkSupplierNameUnique/{supplierName}/{id}", "/checkSupplierNameUnique/{supplierName}"})
    public R checkSupplierNameUnique(@NotBlank(message = "供应商名称不允许为空") @PathVariable("supplierName") String supplierName, @PathVariable(value = "id", required = false) String id) {
        BizSupplier bizSupplier = new BizSupplier();
        bizSupplier.setSupplierName(supplierName);
        bizSupplier.setId(id);
        bizSupplierService.checkSupplierNameUnique(bizSupplier);
        return R.status(Boolean.TRUE);
    }

    /**
     * 导出供应商档案列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizSupplier:export')")
    @Log(title = "供应商档案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizSupplier bizSupplier) {
        List<BizSupplier> list = bizSupplierService.findList(bizSupplier);
        ExcelUtil<BizSupplier> util = new ExcelUtil<BizSupplier>(BizSupplier.class);
        return util.exportExcel(list, "供应商档案数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
